package com.iot08.admin.service;

import com.iot08.admin.mob.MyPushWorkBuilder;
import com.iot08.common.constant.Const;
import com.iot08.common.constant.ConstPush;
import com.iot08.common.entity.admin.MobNotify;
import com.iot08.common.entity.admin.MobNotifyPush;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.DeviceNotification;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @version 1.0
 * @Description: 系统通知相关处理
 * @author: Rock
 * @time: 2020/12/30 9:27
 */
@Service
public class DeviceNotificationService extends BaseService<DeviceNotification> {

    /**
     * @param notify
     * @param user
     * @time: 2020/12/30 9:30
     * @author: Rock
     * @version 1.0
     */
    public DeviceNotification add(MobNotifyPush notifyPush, MobNotify notify, SysUser user) {
        DeviceNotification deviceNotification = new DeviceNotification();
        deviceNotification.setSkipType(notify.getType());
        deviceNotification.setTitle(notify.getTitle());
        deviceNotification.setContent(notify.getContent());
        deviceNotification.setCreater(user.getId());
        deviceNotification.setType(ConstPush.SYSTEM_UPDATE);
        if (MyPushWorkBuilder.TARGET_ALIAS.equals(notifyPush.getTarget())) {
            deviceNotification.setAcceptUser(ConstPush.GROUP);
        } else {
            deviceNotification.setAcceptUser(ConstPush.ALL);
        }
        deviceNotification.setUrl(notify.getLink());
        deviceNotification.setSmallProgramId(notify.getSmallProgramId());
        deviceNotification.setSmallProgramPath(notify.getSmallProgramPath());
        deviceNotification.setBottomBtnImage(notify.getBottomBtnImage());
        deviceNotification.setIncludeQRcodeImage(notify.getIncludeQRcodeImage());
        deviceNotification.setJumpTipsWords(notify.getJumpTipsWords());
        deviceNotification.setAdvertFirm(notify.getAdvertFirm());
        deviceNotification.setCreateTime(new Date());
        //设置为已发布状态
        deviceNotification.setStatus(1);
        //发布时间
        if (Const.INT_1.equals(notifyPush.getTaskCron())
                && notifyPush.getTaskTime() != null && notifyPush.getTaskTime() > 0) {
            deviceNotification.setTime(notifyPush.getTaskTime() / 1000);
        } else {
            deviceNotification.setTime(System.currentTimeMillis() / 1000);
        }
        deviceNotification.setExpiryAt(DateUtil.getSystemExpiryAt());
        return this.save(deviceNotification);
    }

}
